﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;
using System.Data.Objects;

namespace MovieRental.EfRepositoryLibrary.Mappers
{
    public abstract class BaseMapper<TEntity, TKey, TModel, TBase, TObjectContext> 
        : IEntityModelMapper<TEntity, TKey, TModel, TBase, TObjectContext>
        where TEntity : new()
        where TKey : IEquatable<TKey>
        where TModel : EntityObject, new()
        where TBase : EntityObject
        where TObjectContext : ObjectContext
    {
        public abstract void Convert(TEntity entity, TModel model);

        public virtual TModel Convert(TEntity entity)
        {
            var model = new TModel();
            Convert(entity, model);
            return model;
        }

        public virtual TEntity ConvertBack(TModel model)
        {
            var entity = new TEntity();
            ConvertBack(model, entity);
            return entity;
        }

        public abstract TKey SelectKey(TModel model);

        public abstract TKey SelectKey(TEntity entity);

        public virtual IQueryable<TModel> SelectObjectQuery(TObjectContext context)
        {
            return SelectObjectSet(context).OfType<TModel>();
        }

        public abstract ObjectSet<TBase> SelectObjectSet(TObjectContext context);

        public abstract void ConvertBack(TModel model, TEntity entity);
    }
}
